package org.tedu.weibo.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.io.Serializable;

@Configuration
@MapperScan("org.tedu.weibo.mapper")
@EnableSwagger2WebMvc
public class Config {

    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                //用来配置文档信息
                .apiInfo(apiInfo())
                //用于定义为我们项目的哪个包下的类生成文档(通常仅指定controller包)
                .select().apis(RequestHandlerSelectors.basePackage("org.tedu.weibo.controller"))
                //指定为哪些url生成文档，这里为所有
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                //当前项目的名字(生成的文档网页的标题)
                .title("微博项目")
                //当前项目的描述(文档描述)
                .description("微博项目在线API文档")
                //文档版本
                .version("1.0")
                //联系人
                .contact(new Contact("陆铖宇","www.tedu.cn","Lcy@qq.com"))
                //生成文档信息
                .build();
    }

    @Bean
    public RedisTemplate<String, Serializable> initRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        // 创建RedisTemplate对象
        RedisTemplate<String, Serializable> template = new RedisTemplate<>();
        // 设置连接工厂
        template.setConnectionFactory(redisConnectionFactory);
        // 设置key的序列化器
        template.setKeySerializer(RedisSerializer.string());
        // 设置value的序列化器
        template.setValueSerializer(RedisSerializer.json());
        // 设置hashKey的序列化器
        template.setHashKeySerializer(RedisSerializer.json());
        // 设置hashValue的序列化器
        template.setHashValueSerializer(RedisSerializer.json());

        return template;
    }
}
